package edu.uoc.tds.pac4.client.ss3;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.Vector;

import javax.swing.JPanel;

import edu.uoc.tds.pac4.beans.EstPercentatgeAsis;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.common.GestioEventsException;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;


	/**
	 * Pantalla que llista els percentatges d'assistencia
	 */
	public class PantallaPerAssistencia 
		extends PantallaEstadistiques implements PanelClientInterface {

		private static final long serialVersionUID = 1L;
		private JPanel jPanelContainer = null;
		private JPanelFiltrePerAssistencia jPanelFiltre = null;
		private JPanelLlistatPerAssistencia jPanelLlistat = null;
		private JPanelTotalsPerAssistencia jPanelTotals = null;		
		
		/**
		 * Constructor
		 */
		public PantallaPerAssistencia() {
			super();
			initialize();
		}

		/**
		 * Constructor que rep una referencia al servidor remot
		 * 
		 * @param remot
		 */
		public PantallaPerAssistencia(RemoteInterface remot) {
			this.remot = remot;
		}

		/**
		 * Inicialitza la pantalla
		 */
		protected void initialize() {
			
		}

		/**
		 * This method initializes jPanelContainer
		 * 
		 * @return javax.swing.JPanel
		 */
		@Override
		public JPanel getJPanelContainer(Dimension dimension) {
			if (jPanelContainer == null) {
				//Filtre
				GridBagConstraints gbcPanelFiltre = new GridBagConstraints();
				gbcPanelFiltre.gridx = 0;
				gbcPanelFiltre.gridy = 0;
				
				gbcPanelFiltre.fill = GridBagConstraints.VERTICAL;

				//Llistat
				GridBagConstraints gbcPanelLlistat = new GridBagConstraints();
				gbcPanelLlistat.gridx = 0;
				gbcPanelLlistat.gridy = 1;
				gbcPanelLlistat.weightx = 1;
				gbcPanelLlistat.weighty = 1;
				gbcPanelLlistat.insets = new Insets(0, 10, 10, 10);
				gbcPanelLlistat.fill = GridBagConstraints.BOTH;
				
				//Totals
				GridBagConstraints gbcPanelTotals = new GridBagConstraints();
				gbcPanelTotals.gridx = 0;
				gbcPanelTotals.gridy = 2;
				gbcPanelTotals.insets = new Insets(0, 10, 10, 50);
				gbcPanelTotals.anchor = GridBagConstraints.EAST;
				
				
				jPanelContainer = new JPanel();
				jPanelContainer.setSize(dimension);
				jPanelContainer.setLayout(new GridBagLayout());
				jPanelContainer.add(getJPanelFiltre(), gbcPanelFiltre);
				jPanelContainer.add(getJPanelLlistat(), gbcPanelLlistat);
				jPanelContainer.add(getJPanelTotals(), gbcPanelTotals);
			}
			return jPanelContainer;
		}

		/**
		 * This method initializes jPanelFiltre
		 * 
		 * @return javax.swing.JPanel
		 */
		private JPanel getJPanelFiltre() {
			if (jPanelFiltre == null) {
				jPanelFiltre = new JPanelFiltrePerAssistencia(this);

			}
			return jPanelFiltre;
		}


		/**
		 * This method initializes jPanelLlistat
		 * 
		 * @return JPanelLlistat
		 */
		private JPanel getJPanelLlistat() {
			if (jPanelLlistat == null) {
				jPanelLlistat = new JPanelLlistatPerAssistencia();
			}
			return jPanelLlistat;
		}
	
		/**
		 * This method initializes jPanelTotals
		 * 
		 * @return JPaneTotals
		 */
		private JPanel getJPanelTotals() {
			if (jPanelTotals == null) {
				jPanelTotals = new JPanelTotalsPerAssistencia();
			}
			return jPanelTotals;
		}

		
		/**
		 * Actualitza el contingut de la taula
		 */
		public void updateTaula() {
			Date dataInici = jPanelFiltre.getDataInici();
			Date dataFi = jPanelFiltre.getDataFi();
			int perMinim = jPanelFiltre.getPerMinim();
			int perMaxim = jPanelFiltre.getPerMaxim();
			
			// Crida al servidor per rebre el llistat
			Vector<EstPercentatgeAsis> currentLlistat = null;

				try {
					currentLlistat = remot.getEstPercentatgeAsis(jPanelFiltre.getUniversitat(), jPanelFiltre.getCentre(),
							jPanelFiltre.getTipusEvent(), dataInici, dataFi, perMinim, perMaxim);

				} catch (RemoteException e) {
					new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
				} catch (GestioEventsException e) {
					new PantallaError("Error", PantallaError.Tipus.ERROR);
				}
				
				jPanelLlistat.drawEvents(currentLlistat);
				jPanelTotals.drawTotals(currentLlistat);
		}


	}

